package nero.yncc.com.createart.utils;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.OrientationHelper;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import java.util.ArrayList;

import nero.yncc.com.createart.R;
import nero.yncc.com.createart.custom.MyRecyclerView;

/**
 * Created by nero on 17-6-26.
 */

public class LIstPopUtil {

    ArrayList<String> arrayList;

    ListView rc_list;
    SingleTextAdapter adapter;

    Context context;

    PopupWindow popupWindow;
    View view;
    OnItemClickListener listener;

    public LIstPopUtil(Context context, ArrayList<String> arrayList, OnItemClickListener listener) {
        this.arrayList = arrayList;
        this.context = context;
        this.listener = listener;

        view = LinearLayout.inflate(context, R.layout.rc_single, null);
        rc_list = view.findViewById(R.id.rc_list);
        adapter = new SingleTextAdapter(arrayList, context);

        rc_list.setAdapter(adapter);


    }

    class SingleTextAdapter extends BaseAdapter {
        ArrayList<String> arrayList;
        Context context;

        public SingleTextAdapter(ArrayList<String> arrayList, Context context) {
            this.arrayList = arrayList;
            this.context = context;
        }


        @Override
        public int getCount() {
            return arrayList.size();
        }

        @Override
        public Object getItem(int i) {
            return i;
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(final int i, View view, ViewGroup viewGroup) {
            Holder holder;
            if (view == null) {
                view = LayoutInflater.from(context).inflate(R.layout.single_textview, null, false);
                holder = new Holder(view);
                view.setTag(holder);
            } else {
                holder = (Holder) view.getTag();
            }
            holder.textView.setText(arrayList.get(i));
            holder.ll_root.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    listener.Click(i);
                }
            });
            return view;
        }

        class Holder extends RecyclerView.ViewHolder {

            public Holder(View itemView) {
                super(itemView);
                textView = itemView.findViewById(R.id.tv_content);
                ll_root = itemView.findViewById(R.id.ll_root);
            }

            TextView textView;
            LinearLayout ll_root;
        }
    }

    public void flush() {
        adapter.notifyDataSetChanged();
    }

    public void show(View views, PopupWindow.OnDismissListener d) {
        if (popupWindow == null) {
            popupWindow = new PopupWindow(view, views.getWidth(), -2);
            popupWindow.setBackgroundDrawable(new ColorDrawable(0));
            popupWindow.setOutsideTouchable(true);
            popupWindow.setOnDismissListener(d);
        }

        popupWindow.showAsDropDown(views,0,3);
    }

    public void dismiss() {
        if (popupWindow != null)
            popupWindow.dismiss();
    }

    public boolean isShowing() {
        if (popupWindow == null)
            return false;
        return popupWindow.isShowing();
    }

    public interface OnItemClickListener {
        void Click(int position);
    }

}
